#include "../../lvgl/lvgl.h"
#include "ui.h"

int adc_key_fd = -1;
int io_key_fd = -1;

void led_init(void)
{
	int fd;
	
	/* 申请GPIO */
	fd = open("/sys/class/gpio/export", O_WRONLY);
	if (fd != -1) {
		write(fd, "120", 3);
		write(fd, "121", 3);
		write(fd, "123", 3);
		write(fd, "124", 3);
		fsync(fd);
		close(fd);
	}	
	
	/* LED1 */
	fd = open("/sys/class/gpio/gpio120/direction", O_WRONLY);
	if (fd != -1) {
		write(fd, "out", 3);
		fsync(fd);
		close(fd);
	}
	
	/* LED2 */
	fd = open("/sys/class/gpio/gpio121/direction", O_WRONLY);
	if (fd != -1) {
		write(fd, "out", 3);
		fsync(fd);
		close(fd);
	}
	
	/* LED3 */
	fd = open("/sys/class/gpio/gpio123/direction", O_WRONLY);
	if (fd != -1) {
		write(fd, "out", 3);
		fsync(fd);
		close(fd);
	}

	/* LED4 */
	fd = open("/sys/class/gpio/gpio124/direction", O_WRONLY);
	if (fd != -1) {
		write(fd, "out", 3);
		fsync(fd);
		close(fd);
	}	
}

void fd_0_Fun(int fd_Red)
{
        if (fd_Red < 0)
        {
            perror("open RED1 errror");
            return;
        }

        write(fd_Red, "0", 1);
        fsync(fd_Red);
}

void fd_1_Fun(int fd_Red)
{
        if (fd_Red < 0)
        {
            perror("open RED1 errror");
            return;
        }

        write(fd_Red, "1", 1);
        fsync(fd_Red);
}


void keyInit()
{
    //按键初始化
    // adc_key_fd = open(key_io_0, O_RDONLY);
    adc_key_fd = open(key_io_0, O_RDONLY | O_NONBLOCK);
	if (adc_key_fd == -1) {
		perror("Failed to open ADC Key device");
	}

	// io_key_fd = open(key_io_1, O_RDONLY);
    io_key_fd = open(key_io_1, O_RDONLY | O_NONBLOCK);
	if (io_key_fd == -1) {
		perror("Failed to open IO Key device");
	}

}

void user_value_init()
{
	FILE* file = fopen("users_value.csv", "r");
    if (!file) {
        printf("无法打开文件\n");
        return;
    }

	char line[256];

	while (fgets(line, sizeof(line), file)) 
	{
        // 去掉换行符
        line[strcspn(line, "\r\n")] = '\0';

        // 分割字符串
        char* saveptr = NULL;
        char* ID = strtok_r(line, ",", &saveptr);
        char* Passwd = (ID != NULL) ? strtok_r(NULL, ",", &saveptr) : NULL;

		char* home_kongtiao_state = strtok_r(NULL, ",", &saveptr);
		char* home_kongtiao_v = strtok_r(NULL, ",", &saveptr);
		char* home_led_state = strtok_r(NULL, ",", &saveptr);
		char* home_led_v = strtok_r(NULL, ",", &saveptr);
		char* home_chuanglian_state = strtok_r(NULL, ",", &saveptr);
		char* home_chuanglian_v = strtok_r(NULL, ",", &saveptr);
		char* home_jiashiqi_state = strtok_r(NULL, ",", &saveptr);

		char* sleep_kongtiao_state = strtok_r(NULL, ",", &saveptr);
		char* sleep_kongtiao_v = strtok_r(NULL, ",", &saveptr);
		char* sleep_led_state = strtok_r(NULL, ",", &saveptr);
		char* sleep_led_v = strtok_r(NULL, ",", &saveptr);
		char* sleep_chuanglian_state = strtok_r(NULL, ",", &saveptr);
		char* sleep_chuanglian_v = strtok_r(NULL, ",", &saveptr);
		char* sleep_jiashiqi_state = strtok_r(NULL, ",", &saveptr);

		char* windy_kongtiao_state = strtok_r(NULL, ",", &saveptr);
		char* windy_kongtiao_v = strtok_r(NULL, ",", &saveptr);
		char* windy_led_state = strtok_r(NULL, ",", &saveptr);
		char* windy_led_v = strtok_r(NULL, ",", &saveptr);
		char* windy_chuanglian_state = strtok_r(NULL, ",", &saveptr);
		char* windy_chuanglian_v = strtok_r(NULL, ",", &saveptr);
		char* windy_jiashiqi_state = strtok_r(NULL, ",", &saveptr);
		
        if (ID && Passwd &&
            strcmp(lv_textarea_get_text( ta_ID ), ID) == 0 &&
            strcmp(lv_textarea_get_text( ta_Passwd ), Passwd) == 0) {

            fclose(file);
			
			const char *switch_open = "1";
			
			if(strcmp(home_kongtiao_state , switch_open) == 0)
				lv_obj_add_state(home_kongtiao , LV_STATE_CHECKED);
			home_kongtiao_value = atoi(home_kongtiao_v);
			if(strcmp(home_led_state , switch_open) == 0)
				lv_obj_add_state(home_led , LV_STATE_CHECKED);
			home_led_value = atoi(home_led_v);
			if(strcmp(home_chuanglian_state , switch_open) == 0)
				lv_obj_add_state(home_chuanglian , LV_STATE_CHECKED);
			home_chuanglian_value = atoi(home_chuanglian_v);
			if(strcmp(home_jiashiqi_state , switch_open) == 0)
				lv_obj_add_state(home_jiashiqi , LV_STATE_CHECKED);
			
			if(strcmp(sleep_kongtiao_state , switch_open) == 0)
				lv_obj_add_state(sleep_kongtiao , LV_STATE_CHECKED);
			sleep_kongtiao_value = atoi(sleep_kongtiao_v);
			if(strcmp(sleep_led_state , switch_open) == 0)
				lv_obj_add_state(sleep_led , LV_STATE_CHECKED);
			sleep_led_value = atoi(sleep_led_v);
			if(strcmp(sleep_chuanglian_state , switch_open) == 0)
				lv_obj_add_state(sleep_chuanglian , LV_STATE_CHECKED);
			sleep_chuanglian_value = atoi(sleep_chuanglian_v);
			if(strcmp(sleep_jiashiqi_state , switch_open) == 0)
				lv_obj_add_state(sleep_jiashiqi , LV_STATE_CHECKED);

			if(strcmp(windy_kongtiao_state , switch_open) == 0)
				lv_obj_add_state(windy_kongtiao , LV_STATE_CHECKED);
			windy_kongtiao_value = atoi(windy_kongtiao_v);
			if(strcmp(windy_led_state , switch_open) == 0)
				lv_obj_add_state(windy_led , LV_STATE_CHECKED);
			windy_led_value = atoi(windy_led_v);
			if(strcmp(windy_chuanglian_state , switch_open) == 0)
				lv_obj_add_state(windy_chuanglian , LV_STATE_CHECKED);
			windy_chuanglian_value = atoi(windy_chuanglian_v);
			if(strcmp(windy_jiashiqi_state , switch_open) == 0)
				lv_obj_add_state(windy_jiashiqi , LV_STATE_CHECKED);

			lv_label_set_text_fmt(home_kongtiao_txt, "初值:%d°C", home_kongtiao_value);
			lv_label_set_text_fmt(home_LED_txt, "初值:%d%%", home_led_value);
			lv_label_set_text_fmt(home_chuanglian_txt, "初值:%d%%", home_chuanglian_value);
				
			lv_label_set_text_fmt(sleep_kongtiao_txt, "初值:%d°C", sleep_kongtiao_value);
			lv_label_set_text_fmt(sleep_LED_txt, "初值:%d%%", sleep_led_value);
			lv_label_set_text_fmt(sleep_chuanglian_txt, "初值:%d%%", sleep_chuanglian_value);

			lv_label_set_text_fmt(windy_kongtiao_txt, "初值:%d°C", windy_kongtiao_value);
			lv_label_set_text_fmt(windy_LED_txt, "初值:%d%%", windy_led_value);
			lv_label_set_text_fmt(windy_chuanglian_txt, "初值:%d%%", windy_chuanglian_value);

            return;
        }
    }

}

void user_value_modify_init()
{
	FILE* file1 = fopen("users_value.csv", "r");
    if (!file1) {
        printf("无法打开文件\n");
        return;
    }
	FILE* file2 = fopen("users_modify.csv", "w");
    if (!file1) {
        printf("无法打开文件\n");
        return;
    }

	char line[256];
	
	while (fgets(line, sizeof(line), file1)) 
	{
        // 去掉换行符
        line[strcspn(line, "\r\n")] = '\0';

		// 分割字符串
        char* saveptr = NULL;
        char* ID = strtok_r(line, ",", &saveptr);
        char* Passwd = (ID != NULL) ? strtok_r(NULL, ",", &saveptr) : NULL;

		char* home_kongtiao_state = strtok_r(NULL, ",", &saveptr);
		char* home_kongtiao_v = strtok_r(NULL, ",", &saveptr);
		char* home_led_state = strtok_r(NULL, ",", &saveptr);
		char* home_led_v = strtok_r(NULL, ",", &saveptr);
		char* home_chuanglian_state = strtok_r(NULL, ",", &saveptr);
		char* home_chuanglian_v = strtok_r(NULL, ",", &saveptr);
		char* home_jiashiqi_state = strtok_r(NULL, ",", &saveptr);

		char* sleep_kongtiao_state = strtok_r(NULL, ",", &saveptr);
		char* sleep_kongtiao_v = strtok_r(NULL, ",", &saveptr);
		char* sleep_led_state = strtok_r(NULL, ",", &saveptr);
		char* sleep_led_v = strtok_r(NULL, ",", &saveptr);
		char* sleep_chuanglian_state = strtok_r(NULL, ",", &saveptr);
		char* sleep_chuanglian_v = strtok_r(NULL, ",", &saveptr);
		char* sleep_jiashiqi_state = strtok_r(NULL, ",", &saveptr);

		char* windy_kongtiao_state = strtok_r(NULL, ",", &saveptr);
		char* windy_kongtiao_v = strtok_r(NULL, ",", &saveptr);
		char* windy_led_state = strtok_r(NULL, ",", &saveptr);
		char* windy_led_v = strtok_r(NULL, ",", &saveptr);
		char* windy_chuanglian_state = strtok_r(NULL, ",", &saveptr);
		char* windy_chuanglian_v = strtok_r(NULL, ",", &saveptr);
		char* windy_jiashiqi_state = strtok_r(NULL, ",", &saveptr);

		if (ID && Passwd &&
				strcmp(lv_textarea_get_text( ta_ID ), ID) == 0 &&
				strcmp(lv_textarea_get_text( ta_Passwd ), Passwd) == 0)
		{
			if(lv_obj_has_state(home_kongtiao, LV_STATE_CHECKED))
				home_kongtiao_state = "1";
			else
				home_kongtiao_state = "0";
			if(lv_obj_has_state(home_led, LV_STATE_CHECKED))
				home_led_state = "1";
			else
				home_led_state = "0";
			if(lv_obj_has_state(home_chuanglian, LV_STATE_CHECKED))
				home_chuanglian_state = "1";
			else
				home_chuanglian_state = "0";
			if(lv_obj_has_state(home_jiashiqi, LV_STATE_CHECKED))
				home_jiashiqi_state = "1";
			else
				home_jiashiqi_state = "0";

			if(lv_obj_has_state(sleep_kongtiao, LV_STATE_CHECKED))
				sleep_kongtiao_state = "1";
			else
				sleep_kongtiao_state = "0";
			if(lv_obj_has_state(sleep_led, LV_STATE_CHECKED))
				sleep_led_state = "1";
			else
				sleep_led_state = "0";
			if(lv_obj_has_state(sleep_chuanglian, LV_STATE_CHECKED))
				sleep_chuanglian_state = "1";
			else
				sleep_chuanglian_state = "0";
			if(lv_obj_has_state(sleep_jiashiqi, LV_STATE_CHECKED))
				sleep_jiashiqi_state = "1";
			else
				sleep_jiashiqi_state = "0";

			if(lv_obj_has_state(windy_kongtiao, LV_STATE_CHECKED))
				windy_kongtiao_state = "1";
			else
				windy_kongtiao_state = "0";
			if(lv_obj_has_state(windy_led, LV_STATE_CHECKED))
				windy_led_state = "1";
			else
				windy_led_state = "0";
			if(lv_obj_has_state(windy_chuanglian, LV_STATE_CHECKED))
				windy_chuanglian_state = "1";
			else
				windy_chuanglian_state = "0";
			if(lv_obj_has_state(windy_jiashiqi, LV_STATE_CHECKED))
				windy_jiashiqi_state = "1";
			else
				windy_jiashiqi_state = "0";

			fprintf(file2,"%s,%s,%s,%d,%s,%d,%s,%d,%s,%s,%d,%s,%d,%s,%d,%s,%s,%d,%s,%d,%s,%d,%s\n",
				ID, Passwd, home_kongtiao_state, home_kongtiao_value, home_led_state, home_led_value,
				home_chuanglian_state, home_chuanglian_value, home_jiashiqi_state,
				sleep_kongtiao_state, sleep_kongtiao_value, sleep_led_state, sleep_led_value,
				sleep_chuanglian_state, sleep_chuanglian_value, sleep_jiashiqi_state,
				windy_kongtiao_state, windy_kongtiao_value, windy_led_state, windy_led_value,
				windy_chuanglian_state, windy_chuanglian_value, windy_jiashiqi_state);
		}
		else
		{
			fprintf(file2,"%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s\n",
				ID, Passwd, home_kongtiao_state, home_kongtiao_v, home_led_state, home_led_v,
				home_chuanglian_state, home_chuanglian_v, home_jiashiqi_state,
				sleep_kongtiao_state, sleep_kongtiao_v, sleep_led_state, sleep_led_v,
				sleep_chuanglian_state, sleep_chuanglian_v, sleep_jiashiqi_state,
				windy_kongtiao_state, windy_kongtiao_v, windy_led_state, windy_led_v,
				windy_chuanglian_state, windy_chuanglian_v, windy_jiashiqi_state);
		}
	}
	fclose(file1);
	fclose(file2);

	file1 = fopen("users_value.csv", "w");
    if (!file1) {
        printf("无法打开文件\n");
        return;
    }
	file2 = fopen("users_modify.csv", "r");
    if (!file1) {
        printf("无法打开文件\n");
        return;
    }

	while (fgets(line, sizeof(line), file2)) 
	{
        // 去掉换行符
        line[strcspn(line, "\r\n")] = '\0';

		// 分割字符串
        char* saveptr = NULL;
        char* ID = strtok_r(line, ",", &saveptr);
        char* Passwd = (ID != NULL) ? strtok_r(NULL, ",", &saveptr) : NULL;

		char* home_kongtiao_state = strtok_r(NULL, ",", &saveptr);
		char* home_kongtiao_v = strtok_r(NULL, ",", &saveptr);
		char* home_led_state = strtok_r(NULL, ",", &saveptr);
		char* home_led_v = strtok_r(NULL, ",", &saveptr);
		char* home_chuanglian_state = strtok_r(NULL, ",", &saveptr);
		char* home_chuanglian_v = strtok_r(NULL, ",", &saveptr);
		char* home_jiashiqi_state = strtok_r(NULL, ",", &saveptr);

		char* sleep_kongtiao_state = strtok_r(NULL, ",", &saveptr);
		char* sleep_kongtiao_v = strtok_r(NULL, ",", &saveptr);
		char* sleep_led_state = strtok_r(NULL, ",", &saveptr);
		char* sleep_led_v = strtok_r(NULL, ",", &saveptr);
		char* sleep_chuanglian_state = strtok_r(NULL, ",", &saveptr);
		char* sleep_chuanglian_v = strtok_r(NULL, ",", &saveptr);
		char* sleep_jiashiqi_state = strtok_r(NULL, ",", &saveptr);

		char* windy_kongtiao_state = strtok_r(NULL, ",", &saveptr);
		char* windy_kongtiao_v = strtok_r(NULL, ",", &saveptr);
		char* windy_led_state = strtok_r(NULL, ",", &saveptr);
		char* windy_led_v = strtok_r(NULL, ",", &saveptr);
		char* windy_chuanglian_state = strtok_r(NULL, ",", &saveptr);
		char* windy_chuanglian_v = strtok_r(NULL, ",", &saveptr);
		char* windy_jiashiqi_state = strtok_r(NULL, ",", &saveptr);

		fprintf(file1,"%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s\n",
			ID, Passwd, home_kongtiao_state, home_kongtiao_v, home_led_state, home_led_v,
			home_chuanglian_state, home_chuanglian_v, home_jiashiqi_state,
			sleep_kongtiao_state, sleep_kongtiao_v, sleep_led_state, sleep_led_v,
			sleep_chuanglian_state, sleep_chuanglian_v, sleep_jiashiqi_state,
			windy_kongtiao_state, windy_kongtiao_v, windy_led_state, windy_led_v,
			windy_chuanglian_state, windy_chuanglian_v, windy_jiashiqi_state);
	}
	fclose(file1);
	fclose(file2);
}